package service;

import mapper.BrandMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class BrandService {

    //    品牌查询
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize){
        //1.开启会话
        SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession=factory.openSession();
        //    2.获取Mapper
        BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
        //    3.执行分页查询,结果是PageBean<Brand> 类型
        PageBean<Brand> pageBean = new PageBean<>();//分页数据
//       3.1 查询某一页数据
        int beginIndex = (currentPage-1)*pageSize;
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex, pageSize);
        //3.2查询总数据条数
        int totalCount = brandMapper.selectCount();//总数据条数
        //3.3封装分页数据为PangeBean
        pageBean.setData(brandList);
        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        //计算总页数
        int totalPage = (int) Math.ceil( totalCount*1.0/pageSize );
        pageBean.setTotalPage(totalPage);
//    4.sqlSession关闭
        sqlSession.close();
        //    5.返回数据
        return pageBean;
    }

//    品牌查询
    public List<Brand> selectAll(){
    //1.开启会话
    SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
    SqlSession sqlSession=factory.openSession();
    //    2.获取Mapper
    BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
    //    3.执行查询
    List<Brand> brandList=brandMapper.selectAll();
    //    4.sqlSession关闭
    sqlSession.close();
    //    5.返回数据
    return brandList;
    }


    //单个查询
    public Brand selectById(int id){
        //1.开启会话
        SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession=factory.openSession();
        //    2.获取Mapper
        BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
        //    3.执行查询
        Brand brand = brandMapper.selectById(id);
        //    4.sqlSession关闭
        sqlSession.close();
        //    5.返回数据
        return brand;
    }

    //插入一个品牌
    public void add(Brand brand){
        //1.开启会话
        SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession=factory.openSession();
        //    2.获取Mapper
        BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
        //    3.执行查询
        brandMapper.add(brand);
        //提交事务
        sqlSession.commit();
        //    4.sqlSession关闭
        sqlSession.close();

    }

    //修改品牌
    public void update(Brand brand){
        //2. 获取SqlSession
        SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
         mapper.update(brand);
        // 提交事务
         sqlSession.commit();
        // 释放资源
         sqlSession.close();
         }

    //删除品牌
    public void delete(int id){
        //2. 获取SqlSession
        SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        // 3. 获取BrandMapper
         BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        // 4. 调用方法
         mapper.deleteById(id);
        // 提交事务
         sqlSession.commit();
        // 释放资源
         sqlSession.close();
         }


    //批量删除品牌
    public void deletes(int[] ids){
        //2. 获取SqlSession
        SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        // 3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        // 4. 调用方法
        mapper.deleteByIds(ids);
        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }
}
